package co.yixiang.common.security.service;

import co.yixiang.common.core.api.ApiResult;
import co.yixiang.common.core.constant.SecurityConstants;
import co.yixiang.common.core.utils.ServletUtils;
import co.yixiang.mall.common.mall.domain.YxUser;
//import co.yixiang.upms.common.system.dto.UserDto;
//import co.yixiang.upms.common.system.vo.UserInfo;
import co.yixiang.retail.common.domain.UserInfo;
import co.yixiang.retail.common.dto.UserDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hupeng on 2020/9/25.
 */
@Component
public class UsernameUserDetailService extends BaseUserDetailService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected UserInfo getUser(String username) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest();
        String loginType = (String) httpServletRequest.getAttribute(SecurityConstants.LOGIN_TYPE);
        if (StringUtils.equals(loginType, SecurityConstants.APP_LOGIN)) {
            ApiResult<YxUser> apiResult = remoteMemberUserService.getUserInfo(username);
            UserInfo myUserInfo = new UserInfo();
            myUserInfo.setPermissions(null);
            YxUser yxUser = apiResult.getData();

            UserDto userDto = new UserDto();
            userDto.setId(yxUser.getUid());
            userDto.setUsername(yxUser.getUsername());
            userDto.setNickName(yxUser.getNickname());
            userDto.setSex("");
            userDto.setPassword(yxUser.getPassword());
            userDto.setAvatar(yxUser.getAvatar());
            userDto.setEmail("");
            userDto.setPhone(yxUser.getPhone());
            userDto.setEnabled(true);

            myUserInfo.setUserDto(userDto);

            System.out.println("myUserInfo:"+myUserInfo);

            return myUserInfo;

        }
//        ApiResult<co.yixiang.upms.common.system.vo.UserInfo> userInfo = remoteUserService.getUserInfo(username);
//        co.yixiang.upms.common.system.dto.UserDto user = userInfo.getData().getUserDto();
        //UserDto user = null;
//        if(user == null){
//            logger.error("找不到该用户，用户名：" + username);
//            throw new UsernameNotFoundException("找不到该用户，用户名：" + username);
//        }

        return null;
//        return userInfo.getData();
    }
}
